<?php

namespace app\admin\controller\exam;

use app\common\controller\Backend;

use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use push\Push;
use app\common\model\User;

/**
 * 学员模拟考试记录
 *
 * @icon fa fa-circle-o
 */
class GradeLog extends Backend
{

    /**
     * Log模型对象
     * @var \app\common\model\GradeLog
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\GradeLog;

        $this->view->assign("levelList", $this->model->getLevelList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


     /**
      * 专家点评
      *
      * @return void
      */
    public function comment($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }

        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if(empty($params['explain'])){
                return $this->error('请填写点评');
            }
            if ($params) {
                $params = $this->preExcludeFields($params);
                $result = false;
                Db::startTrans();
                try {

                    // 验证提交数据
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (\Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {

                    // APP push推送
                    try {

                        $user_id       = $row['user_id'];
                        $push_clientid = User::where('id',$user_id)->value('push_clientid');
                        if(!empty($push_clientid)){
                            $param   = [
                                'title'         => date('Y-m-d H').' 您的模拟考试专家已点评',
                                'push_clientid' => $push_clientid,
                                'content'       => $params['explain'],
                                'payload'       => [
                                    'text'      => $params['explain'],
                                    'id'        => $row['id'],  //  模拟考成绩ID
                                    'type'      => 1            //  专家点评类型 
                                ]
                            ];
                            $param  = json_encode($param);
                            Push::send($param);
                        }
                    } catch (\Exception $e) {
                    }

                    return $this->success('点评成功','exam/grade_log/comment?ids='.$ids);
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
    
    
}
